package com.asdan.seededu.web.controller;


import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asdan.seededu.entity.security.SubProject;
import com.asdan.seededu.entity.security.SubProjectStep;
import com.asdan.seededu.entity.security.User;
import com.asdan.seededu.entity.security.Userdata;
import com.asdan.seededu.service.ProjectService;
import com.asdan.seededu.service.StudentService;
import com.asdan.seededu.service.impl.ProjectServiceImpl;
import com.asdan.seededu.service.impl.StudentServiceImpl;
import com.asdan.seededu.util.Pager;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-project")
@RequestMapping("projects")
public class ProjectController {
	/**
	 * Display project page
	 * 
	 * @return
	 */
	@RequestMapping("")
	public String projects() {
		return "projects";
	}

	@RequestMapping("/question.txt")
	public String file() {
		return "question.txt";
	}
	
	@RequestMapping(value="/addUsertoSp",method=RequestMethod.POST)
	public String addSubPro(String stepid ,String spid ,String fpid ,@RequestParam("selectUser2") String[] uids ,Model model) {
		List<User> listuser = new  ArrayList<User>()  ;
		for(int i=0;i<uids.length;i++){
			StudentService ss = new StudentServiceImpl() ; 
			listuser.add(ss.getStudentByEmailOrID("", Integer.parseInt(uids[i]))) ;
		}
		ProjectService ps = new ProjectServiceImpl() ; 
		if(ps.addUsertoSubProject(Integer.parseInt(stepid), Integer.parseInt(spid), listuser)){
			model.addAttribute("message", "添加用户到子项目成功！") ;
		}else{
			model.addAttribute("message", "添加用户到子项目失败！") ;
		}
		return showspList(fpid, spid, model) ;
		
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addSubPro(String fpid ,String spid , Model model) {
		
		
		return "" ;
	}
	
	@RequestMapping(value="/showspList",method=RequestMethod.GET) 
	public String showspList(String fpid ,String spid,Model model){
		
		System.out.println("showspList");
		ProjectService ps = new ProjectServiceImpl() ; 
		List<SubProject> listPro  ;
		model.addAttribute("fpid",fpid) ;
		if(spid!=null&&!"".equals(spid)){
			listPro = ps.getProjects("", spid) ;
			ps = new ProjectServiceImpl() ; 
			List<SubProjectStep> spsList = ps.getProjectStep(Integer.parseInt(spid) ) ;
			System.out.println(spsList.get(0).getAttachment1name()+"  -------"+spsList.get(0).getAttachment1address()) ; 
			model.addAttribute("spsList", spsList) ;
			
			ps = new ProjectServiceImpl() ;
			Pager<Userdata> page = ps.getUsersbyPro(spid, "", "", "","", "", 1);
			
			model.addAttribute("list", page.getDatas());
			model.addAttribute("pageNo",page.getOffset());
			model.addAttribute("pageCount", page.getTotalPage());
			model.addAttribute("totalCount",page.getTotal());
			model.addAttribute("size", page.getSize()) ;
		}else{
			listPro = ps.getProjects(fpid, "") ;
		}
		
		if("1".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/silkroad1" ;
			}else{
				model.addAttribute("sl", listPro) ;
			}
		}else if("2".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/hainan1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("3".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/onlinebusinesssimulation1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("4".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/businesssimulation1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("5".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/aapt1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("6".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/aaptforeign1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("7".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/wyef1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("8".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/math1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("9".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/mathforeign1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
			
		}else if("10".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/mun1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
		}else if("11".equals(fpid)){
			if(spid!=null&&!"".equals(spid)){
				model.addAttribute("sp", listPro.get(0)) ;
				return "projects/minimba1";
			}else{
				model.addAttribute("sl", listPro) ;
			}
		}
		return "projects/subprojects" ;
		
	}	
	
	@RequestMapping(value="/search-project-signature",method=RequestMethod.GET,produces="test/html;charset=UTF-8")
	@ResponseBody
	public String searchPro(String name,Model model){
		System.out.println(name);
		ProjectService ps = new ProjectServiceImpl() ; 
		List<SubProject> sublist = ps.searchPro(name) ;
		System.out.println(JsonUtils.toJsonString(sublist));
		return JsonUtils.toJsonString(sublist) ;
	}	
	
	@RequestMapping(value="/showUserBysubpage",method=RequestMethod.POST,produces="test/html;charset=UTF-8")
	@ResponseBody
	public String showUserBysubpage(String spid ,String name,String schoolId  ,String cellPhone ,String subProId,String email,String pageNo,Model model){
		System.out.println(name);
		System.out.println(spid) ;
		System.out.println(pageNo);
		ProjectService ps = new ProjectServiceImpl() ;
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		Pager<Userdata> page = ps.getUsersbyPro(spid, name, schoolId, cellPhone,email, "", Integer.parseInt(pageNo));
		System.out.println(JsonUtils.toJsonString(page));
		return JsonUtils.toJsonString(page) ;
	}	
	@RequestMapping(value="/delUserfromSub",method=RequestMethod.POST,produces="test/html;charset=UTF-8")
	@ResponseBody
	public String delUserfromSub(String spid ,String uid ,Model model){
		System.out.println(spid) ;
		System.out.println(uid);
		ProjectService ps = new ProjectServiceImpl() ;
		if(ps.delUserfromSub(Integer.parseInt(spid), Integer.parseInt(uid))){
			return "1" ;
		}else{
			return "0" ;
		}
	}
	
	
}
